/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import java.lang.reflect.InvocationTargetException;

import net.cellobject.javacodeformatter.log.FormatterLog;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * A do nothing handler showing simulated progress in the window's status bar
 */
public class SimulatedOperationHandler extends AbstractHandler {
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		try {
			window.run(true, true, new IRunnableWithProgress() {
				public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					monitor.beginTask("simulate status bar progress:", 20);
					for (int i = 20; i > 0; --i) {
						monitor.subTask("seconds left = " + i);
						Thread.sleep(1000);
						monitor.worked(1);
					}
					monitor.done();
				}
			});
		} catch (final InvocationTargetException e) {
			FormatterLog.logError(e);
		} catch (final InterruptedException e) {
			// User canceled the operation... just ignore.
		}
		return null;
	}
}
